package com.gpay.erp.web.control;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;
import com.gpay.erp.core.system.bean.SysUser;
import com.gpay.erp.core.system.bean.TreeNode;
import com.gpay.erp.services.system.LoginInteface;
import com.gpay.erp.web.util.WebUtil;
import com.gpay.erp.web.vo.ResponseInfo;
import com.gpay.erp.web.vo.UserSessionInfor;

/**
 * 用户登录管理
 * 
 * @author lg
 * 
 */
@Controller
@RequestMapping("/loginController/")
public class LoginController extends BaseController {
	private Log log = LogFactory.getLog(LoginController.class);
	@Autowired
	private LoginInteface loginService;

	// 主页面
	@RequestMapping("index")
	public String index(HttpServletRequest request, HttpServletResponse response) {
		return "index";
	}

	/*
	 * 获取用户资源权限
	 * 
	 * @request
	 * 
	 * @response
	 */
	@RequestMapping("tree")
	public void tree(HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		HttpSession session = request.getSession();
		ResponseInfo responseInfo = new ResponseInfo();
		SysUser sysuser = new SysUser();
		/* 获取用户登录信息 */
		if (session.getAttribute("loginUser") != null) {
			sysuser = (SysUser) session.getAttribute("loginUser");
			/* 查询用户权限，放入list集合 */
			List<TreeNode> list = TreeNode.formatTree(loginService
					.menuSelect(sysuser));
			if (list.size() > 0) {
				responseInfo.setResult("10010");
				responseInfo.setMessage(requestContext
						.getMessage("privilege.menu.success"));
				responseInfo.setObject(list);
			} else {
				responseInfo.setResult("10011");
				responseInfo.setMessage(requestContext
						.getMessage("privilege.menu.failed"));
			}
		} else {
			responseInfo.setResult("10002");
			responseInfo.setMessage(requestContext.getMessage("login.timeout"));

		}
		WebUtil.responseToJson(response, responseInfo);
	}

	// 用户登录界面
	@RequestMapping("loginPage")
	public String loginPage(HttpServletRequest request,
			HttpServletResponse response) {
		return "login";

	}

	/*
	 * 用户登录
	 * 
	 * @userSessionInfor
	 * 
	 * @request
	 * 
	 * @response
	 */
	@RequestMapping(value = "login")
	public void login(
			@ModelAttribute("userSessionInfor") UserSessionInfor userSessionInfor,
			SysUser sysuser, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		/* 将用户名密码放入用户实体类中 */
		sysuser.setUserName(userSessionInfor.getUsername());
		sysuser.setUserPassword(userSessionInfor.getPasswd());
		sysuser.setStatus(userSessionInfor.getStatus());
		/* 判断用户名和密码是否正确 */
		sysuser = loginService.login(sysuser);
		if (sysuser != null && sysuser.getStatus().equals("0")) {
			log.info("userName:" + sysuser.getUserName());
			log.info("userPassword:" + sysuser.getUserPassword());
			responseInfo.setResult("10000");
			responseInfo.setMessage(requestContext.getMessage("login.success"));
			session.setAttribute("loginUser", sysuser);
		} else {
			responseInfo.setResult("10001");
			responseInfo.setMessage(requestContext
					.getMessage("username.or.password.mistake"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}

	// 页面跳转 用户密码修改
	@RequestMapping("userEditPwd")
	public String userEditPwd() {
		return "user/userEditPwd";
	}

	/*
	 * 点击密码修改，进行密码修改
	 * 
	 * @oldPwd:旧密码
	 * 
	 * @pwd:新密码
	 * 
	 * @rePwd:确定密码
	 */
	@RequestMapping("updatePwd")
	public void userEditPwd(SysUser sysuser, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResponseInfo responseInfo = new ResponseInfo();
		HttpSession session = request.getSession();
		/* 获取密码修改的信息 */
		String oldPwd = request.getParameter("oldPwd");
		String pwd = request.getParameter("pwd");
		String rePwd = request.getParameter("rePwd");
		/* 判断用户是否存在 */
		if (session.getAttribute("loginUser") != null) {
			sysuser = (SysUser) session.getAttribute("loginUser");
			if (sysuser.getUserPassword() != null
					&& sysuser.getUserPassword().equals(oldPwd)) {
				sysuser.setUserPassword(pwd);
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");// 设置日期格式
				sysuser.setCreatedate(df.format(new Date()));
				log.info("当前时间：" + df.format(new Date()));
				if (loginService.updatePwd(sysuser) != 0) {
					responseInfo.setResult("10020");
					responseInfo.setMessage(requestContext
							.getMessage("user.updatepwd.success"));
				}
			} else {
				responseInfo.setResult("10003");
				responseInfo.setMessage(requestContext
						.getMessage("user.passwd.error"));
			}
		} else {
			responseInfo.setResult("10021");
			responseInfo.setMessage(requestContext
					.getMessage("user.updatepwd.failed"));
		}
		WebUtil.responseToJson(response, responseInfo);
	}
}